/* 
	author: istockphp.com
*/
jQuery(function($) {
	
	$("a.topopup").click(function() {
			loading(); // loading
			setTimeout(function(){ // then show popup, deley in .5 second
				loadPopup(); // function show popup 
			}, 500); // .5 second
	return false;
	});
	
	/* event for close the popup */
	$("div.close").hover(
					function() {
						$('span.ecs_tooltip').show();
					},
					function () {
    					$('span.ecs_tooltip').hide();
  					}
				);
	
	$("div.close").click(function() {
		disablePopup();  // function close pop up
	});
	
	$(this).keyup(function(event) {
		if (event.which == 27) { // 27 is 'Ecs' in the keyboard
			disablePopup();  // function close pop up
		}  	
	});
	
	$('a.livebox').click(function() {
		alert('Hello World!');
	return false;
	});
	

	 /************** start: functions. **************/
	function loading() {
		$("div.loader").show();  
		var type;
		var res_pic = /^pic\./i;
		var res_book = /^book\./i;
		var res_video = /^video\./i;
		var res_express = /^express\./i;
		var res_recommendation = /^recommendation\./i;
		if (event.srcElement.id.match(res_pic))
			type='pics';
		else if (event.srcElement.id.match(res_book))
			type = 'books';
		else if (event.srcElement.id.match(res_video))
			type = 'videos';
		else if (event.srcElement.id.match(res_express))
			type = 'expresses';
		else if (event.srcElement.id.match(res_recommendation))
			type = 'recommendations';
		else
			type = 'others';
		
		var field = event.srcElement.id.substring(event.srcElement.id.indexOf('.') + 1);
		$('#fileTreeDemo_1').fileTree({ root: '/', script: 'services/api/files/' + type }, field, function(file, f) { 
			document.getElementById(f).value=file;
			disablePopup();
	    });
	}
	function closeloading() {
		$("div.loader").fadeOut('normal');  
	}
	
	var popupStatus = 0; // set value
	
	function loadPopup() { 
		if(popupStatus == 0) { // if value is 0, show popup
			closeloading(); // fadeout loading
			$("#toPopup").fadeIn(0500); // fadein popup div
			$("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
			$("#backgroundPopup").fadeIn(0001); 
			popupStatus = 1; // and set value to 1
		}	
	}
		
	function disablePopup() {
		if(popupStatus == 1) { // if value is 1, close popup
			$("#toPopup").fadeOut("normal");  
			$("#backgroundPopup").fadeOut("normal");  
			popupStatus = 0;  // and set value to 0
		}
	}
	/************** end: functions. **************/
}); // jQuery End